<?php
include('include/db.inc.php');
include('cfg/cfg.inc.php');

function sqlValue($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
    $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

    switch ($theType) {
        case "text":
            $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
            break;
        case "int":
            $theValue = ($theValue != "") ? intval($theValue) : "NULL";
            break;
        case "double":
            $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
            break;
        case "date":
            $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
            break;
        case "defined":
            $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
            break;
    }
    return $theValue;
}

$db = new DbManager();
$db->connect(DBHOST, DBUSER, DBPWD, true, true, DBNAME, "");
$userName = sqlValue($user->name, "text");
$opciones = "select cllg_menu.TITTLE,cllg_menu.ID from cllg_acces,cllg_menu,cllg_user,cllg_perfil
where cllg_user.USER_NAME = $userName
 and cllg_user.FK_PERFIL = cllg_acces.FK_PERFIL
 and cllg_acces.FK_MENU = cllg_menu.ID
 and cllg_acces.status=1
 and cllg_menu.ORDEN = 0 group by cllg_menu.id";

$rsOpciones = $db->queryToArray($opciones);
?>
<ul id="menuprincipal" class="MenuBarHorizontal">
    <?php
    foreach ($rsOpciones as $opcion) {
        $id = $opcion["ID"];
        ?>
        <li><a class="MenuBarItemSubmenu" href="#"><?PHP echo $opcion["TITTLE"]; ?></a>
            <?PHP
            $querySubOpciones = "select cllg_menu.URL,cllg_menu.TITTLE,cllg_menu.ID,cllg_menu.MENU_PARENT
                                                                    from
                                                                    (select * from cllg_acces a where a.status=1) acces,cllg_menu,cllg_user,cllg_perfil
                                                                    where cllg_user.USER_NAME like $userName
                                                                    and cllg_user.FK_PERFIL = acces.FK_PERFIL
                                                                    and cllg_menu.ID = acces.FK_MENU
                                                                    and cllg_menu.ORDEN = '$id'
                                                                    and cllg_menu.MENU_PARENT <= 2 group by cllg_menu.id";
            $rsSubOpciones = $db->queryToArray($querySubOpciones);
            ?>
            <ul>

                <?php
                foreach ($rsSubOpciones as $subOpcion) {
                    if ($subOpcion["MENU_PARENT"] >= 2) {
                        $Subsuba = "SI";
                    } else {
                        $Subsuba = "NO";
                    }
                    ?>
                    <?PHP if ($Subsuba == "NO") { ?>
                        <li><a href="<?php print $subOpcion["URL"] ?>"><?php print $subOpcion["TITTLE"] ?></a></li>		  				 				<?PHP } ?>
                        <?PHP if ($Subsuba == "SI") { ?>
                        <li><a class="MenuBarItemSubmenu" href="#"><?PHP echo $subOpcion["TITTLE"]; ?></a>
                            <?PHP
                            $idmenu = $subOpcion["ID"];
                            $Querry_recordSetSUb = "select *
                                    from cllg_menu,cllg_user,(select * from cllg_acces a where a.status=1) acces
                                    where cllg_user.USER_NAME like $userName
                                          and cllg_user.FK_PERFIL = acces.FK_PERFIL
                                          and cllg_menu.ID = acces.FK_MENU
                                          AND ORDEN = '$idmenu'";
                            $recordsetSUb = $db->queryToArray($Querry_recordSetSUb);
                            ?>
                            <ul>
                                <?PHP foreach ($recordsetSUb as $pruebaSUb) { ?>
                                    <li><a href="<?php echo $pruebaSUb["URL"]; ?>"><?PHP echo $pruebaSUb["TITTLE"]; ?></a></li>
            <?PHP } ?>
                            </ul>
                        </li>
                    <?PHP } ?>
    <?PHP } ?>
            </ul>

        </li>
        <?PHP
    }
    ?>
</ul>


